package com.atguigu.guli.service.ucenter.controller.api;

import com.atguigu.guli.service.base.dto.UserDto;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.base.utils.JwtHelper;
import com.atguigu.guli.service.base.utils.JwtInfo;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.form.ApiMemberForm;
import com.atguigu.guli.service.ucenter.entity.form.LoginForm;
import com.atguigu.guli.service.ucenter.service.MemberService;
import com.atguigu.utils.FormUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/ucenter/member")

public class ApiMemberController {
    @Autowired
    MemberService memberService;

    //trade查询会员的信息
    @GetMapping("getUserDto/{memberId}")
    public R getUserDto(@PathVariable String memberId) {
        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Member::getId,Member::getMobile,Member::getNickname) ;
        queryWrapper.eq(Member::getId,memberId) ;
        Member member = memberService.getOne(queryWrapper);
        UserDto memberDto = new UserDto() ;
        BeanUtils.copyProperties(member,memberDto);

        return R.ok().data("item",memberDto) ;
    }

    //4、根据tokne获取登录成功用户信息
    @GetMapping("getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        //从浏览器提交的报文中获取到token，解析token
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);//工具类JwtHelper从请求对象中的请求头中获取token头的值
        return R.ok().data("item" , jwtInfo);
    }
    //3.登录
    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginForm loginForm) {
        String mobile = loginForm.getMobile();
        String password = loginForm.getPassword();

        ///校验参数
        if (StringUtils.isEmpty(mobile)
                || !FormUtils.isMobile(mobile)
                || StringUtils.isEmpty(password)) {
            return R.setResult(ResultCodeEnum.PARAM_ERROR);
        }
        String token = memberService.login(loginForm) ;
        return R.ok().data("token",token).message("登录成功") ;
    }

    //2.判断手机号码是否注册
    @GetMapping("isRegisted/{mobile}")
    public R isRegisted(@PathVariable String mobile) {
        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Member::getMobile, mobile);

        return R.ok().data("isRegisted", memberService.count(queryWrapper) > 0 ? "1" : "0");
    }

    //1.注册
    @PostMapping("regist")
    public R regist(@RequestBody ApiMemberForm apiMemberForm) {
        memberService.saveMember(apiMemberForm);
        return R.ok();
    }
}
